package cn.amaake.service;

import com.jfinal.plugin.activerecord.Page;

import cn.amaake.common.model.PowerRole;
import cn.amaake.common.model.Role;

import java.util.List;

public class RoleServiceImp implements RoleService {
    @Override
    public Page<Role> getRolePage(int pageNumber,int pageSize,String name) {
        return Role.dao.getMenuPage(pageNumber,pageSize,name);
    }

    @Override
    public boolean addRole(Role role) {
        return role.save();
    }

    @Override
    public boolean updateRole(Role role) {
        return role.update();
    }

    @Override
    public boolean deleteRole(int id) {
        //MenuRole.dao.deleteMenuRole(id);
    	PowerRole.dao.deletePowerRole(id);
        return Role.dao.deleteById(id);
    }

    @Override
    public boolean accredit(String roleid, String[] menuIds) {
        PowerRole.dao.deletePowerRole(Integer.parseInt(roleid));
        PowerRole powerRole = null;
        boolean boo = true;
        for(String menuid : menuIds){
            if(menuid.contains("-")){
                powerRole = new PowerRole();
                powerRole.setPowerid(Integer.parseInt(menuid.split("-")[1]));
                powerRole.setRoleId(Integer.parseInt(roleid));
                boo = powerRole.save();
            }
        }
        return boo;
    }

    @Override
    public List<Role> getRoleList() {
        return Role.dao.getRoleList();
    }
}
